﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Big_Data.View
{
    public partial class FrmInstallInformationEdit : Form
    {
        DataBase userDB = new DataBase();

        public string _ass_no;

        public FrmInstallInformationEdit(string ass_no)
        {
            this._ass_no = ass_no;
            InitializeComponent();
        }

        /// <summary>
        /// 初始化字表编辑页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmInstallInformationDetailEdit_Load(object sender, EventArgs e)
        {
            try
            {

                InitDataBind();
                if (!string.IsNullOrEmpty(_ass_no))
                {
                    this.Text = "修改安装信息数据-【" + _ass_no + "】";
                    string sqlstr = "select * from T_assemble where ass_no='" + this._ass_no + "'";
                    DataTable dataTable = userDB.show(sqlstr);
                    if (dataTable.Rows.Count > 0)
                    {
                        DataRow dr = dataTable.Rows[0];
                        this.txtAss_no.Text = dr["Ass_no"].ToString();
                        this.cbbSta_code.SelectedValue = dr["Sta_code"].ToString();
                        this.dtpAss_date.Value = Convert.ToDateTime(dr["Ass_date"].ToString());
                        this.txtAss_corp.Text = dr["Ass_corp"].ToString();
                        this.txtAss_check.Text = dr["Ass_check"].ToString();
                        this.txtAss_in.Text = dr["Ass_in"].ToString();
                        this.dtpRec_date.Value = Convert.ToDateTime(dr["Rec_date"].ToString());
                        this.cbbEng_code.SelectedValue = dr["Eng_code"].ToString();
                        this.cbbDis_code.SelectedValue = dr["Dis_code"].ToString();
                    }
                    else
                    {
                        this.Text = "新增安装信息数据";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("初始化失败：" + ex.Message);
            }
        }


        private void InitDataBind()
        {
            //加油站表
            string S_station_sql = "select * from S_station";
            DataTable dt_S_station = userDB.show(S_station_sql);
            this.cbbSta_code.ValueMember = "Sta_code";
            this.cbbSta_code.DisplayMember = "Sta_name";
            this.cbbSta_code.DataSource = dt_S_station;

            //工程师表
            string S_engineer_sql = "select * from S_engineer";
            DataTable dt_S_engineer = userDB.show(S_engineer_sql);
            this.cbbEng_code.ValueMember = "Eng_code";
            this.cbbEng_code.DisplayMember = "Eng_name";
            this.cbbEng_code.DataSource = dt_S_engineer;

            //分销商（服务商）表
            string S_distributor_sql = "select * from S_distributor";
            DataTable dt_S_distributor = userDB.show(S_distributor_sql);
            this.cbbDis_code.ValueMember = "Dis_code";
            this.cbbDis_code.DisplayMember = "Dis_name";
            this.cbbDis_code.DataSource = dt_S_distributor;
        }

        /// <summary>
        /// 保存时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string sqlstr = "";
                var _Ass_no = this.txtAss_no.Text;
                var _Sta_code = this.cbbSta_code.SelectedValue;
                var _Ass_date = this.dtpAss_date.Value.ToString("yyyy-MM-dd HH:mm:ss");
                var _Ass_corp = this.txtAss_corp.Text;
                var _Ass_check = this.txtAss_check.Text;
                var _Ass_in = this.txtAss_in.Text;
                var _Rec_date = this.dtpRec_date.Value.ToString("yyyy-MM-dd HH:mm:ss");
                var _Eng_code = this.cbbEng_code.SelectedValue;
                var _Dis_code = this.cbbDis_code.SelectedValue;

                if (!string.IsNullOrEmpty(_ass_no))
                {
                    sqlstr = string.Format("update T_assemble set Sta_code='{1}',Ass_date='{2}',Ass_corp='{3}',Ass_check='{4}',Ass_in='{5}',Rec_date='{6}',Eng_code='{7}',Dis_code='{8}' where Ass_no='{9}'",
                        _Ass_no, _Sta_code, _Ass_date, _Ass_corp, _Ass_check, _Ass_in, _Rec_date, _Eng_code, _Dis_code, _Ass_no);
                }
                else
                {
                    sqlstr = string.Format("insert into T_assemble(Ass_no,Sta_code,Ass_date,Ass_corp,Ass_check,Ass_in,Rec_date,Eng_code,Dis_code) "
                        + " values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')",
                        _Ass_no, _Sta_code, _Ass_date, _Ass_corp, _Ass_check, _Ass_in, _Rec_date, _Eng_code, _Dis_code);
                }
                if (userDB.dosqlcom(sqlstr) > 0)
                {
                    this.DialogResult = DialogResult.OK;
                    MessageBox.Show("保存成功！");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("保存失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("异常：" + ex.Message);
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
